package org.jeecg.modules.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetUseridByUnionidRequest;
import com.dingtalk.api.request.OapiUserUpdateRequest;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetUseridByUnionidResponse;
import com.dingtalk.api.response.OapiUserUpdateResponse;
import com.dingtalk.oapi.lib.aes.DingTalkEncryptException;
import com.taobao.api.ApiException;
import org.jeecg.common.api.ding.DingApi;
import org.jeecg.modules.config.URLConstant;
import org.jeecg.modules.dinguser.entity.DiyDingUser;
import org.jeecg.modules.dinguser.service.IDiyDingUserService;
import org.jeecg.modules.service.CallbackService;
import org.jeecg.modules.util.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class UserUtil {
    private static IDiyDingUserService dingUserService;

    @Autowired
    public void setDingUserService(IDiyDingUserService dingUserService) {
        UserUtil.dingUserService = dingUserService;
    }

    /**
     * 更新钉钉用户的信息
     * @param userId
     */
    public static void updateDingUser(String userId) throws ApiException {
       updateDingUser(userId,630604636L);
    }

    /**
     * 更新钉钉用户信息
     * @param userId
     * @param type 成员所属部门
     * @throws ApiException
     */
    public static void updateDingUser(String userId,long type) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/update");
        OapiUserUpdateRequest request = new OapiUserUpdateRequest();
        request.setUserid(userId);
        request.setDepartment(Arrays.asList(type));
        OapiUserUpdateResponse response = client.execute(request, AccessTokenUtil.getToken());
    }
    public static String unionIdChageUserId(String unionId){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getUseridByUnionid");
        OapiUserGetUseridByUnionidRequest request = new OapiUserGetUseridByUnionidRequest();
        request.setUnionid(unionId);
        request.setHttpMethod("GET");
        try {
            OapiUserGetUseridByUnionidResponse response = client.execute(request, AccessTokenUtil.getToken());
            //0表示企业内部员工，1表示企业外部联系人
            return response.getUserid();
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String contactTypebyUnionid(String unionId){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getUseridByUnionid");
        OapiUserGetUseridByUnionidRequest req = new OapiUserGetUseridByUnionidRequest();
        req.setUnionid(unionId);
        req.setHttpMethod("GET");
        try {
            OapiUserGetUseridByUnionidResponse response = client.execute(req, AccessTokenUtil.getToken());
            return response.getUserid().toString();
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static boolean getuserinfobyuserid(String userid) throws ApiException {
        boolean falg=false;
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
        OapiUserGetRequest req = new OapiUserGetRequest();
        req.setUserid(userid);
        req.setHttpMethod("GET");
        OapiUserGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        List<Long> department = rsp.getDepartment();
        System.out.println("部门----》"+department);
        for (Long dep:department) {
            if(dep.equals(631399165)||dep.equals(630604636)||dep==631399165||dep==630604636||dep==631446106||dep.equals(631446106)){
                falg=true;
            }
        }
        return falg;
    }

    /**
     * 获取用户详情
     * @param accessToken
     * @param userId
     * @return
     */
    public static OapiUserGetResponse getUserProfile(String accessToken, String userId) {
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_USER_GET);
            OapiUserGetRequest request = new OapiUserGetRequest();
            request.setUserid(userId);
            request.setHttpMethod("GET");
            OapiUserGetResponse response = client.execute(request, accessToken);

            return response;
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }
}
